package semiProject_army.mvc.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import semiProject_army.mvc.beans.MemberDAO;

public class LoginAction implements ActionHandler {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		req.setCharacterEncoding("UTF-8");
		String isLogin = req.getParameter("isLogin");
		HttpSession session = req.getSession();
		if(isLogin == null){
			isLogin = "false";
		}
		int result = -1;
		System.out.println(isLogin);

		
		if(isLogin.equals("true"))
		{
			
			session.invalidate();
			/*session.removeAttribute("isLogin");
			session.removeAttribute("userId");
			session.removeAttribute("userPw");
			
			
			session.setAttribute("isLogin", null);
			session.setAttribute("isLogout", null);
			session.setAttribute("result", 0);*/
			req.setAttribute("action", "index");
		}
		else{		

		String userId = req.getParameter("userId");
		String userPw = req.getParameter("userPw");
			
		
		System.out.println("유저아이디 :" + userId);
		System.out.println("유저비밀번호 :" + userPw);
		

		MemberDAO dao = MemberDAO.getInstance();
			
		result = dao.checkMember(userId,userPw);
		
		
		req.setAttribute("result", result);
		
				if(result==1){
					req.setAttribute("action", "login/loginok");
					session.setAttribute("isLogin", true);
					session.setAttribute("userId", userId);
					session.setAttribute("userPw", userPw);
					session.setMaxInactiveInterval(60*30);
				}else if(userId==null&&userPw==null){
					req.setAttribute("action", "index");
				}
				else{
					req.setAttribute("action", "login/loginok");
				}
		}
		System.out.println(result);
		
		return viewPage;	
	}
}